public class Test {

    //快乐数
    /*
    * 快慢双指针：先判断是否有环
    * 1.定义快慢指针
    * 2.慢指针每次向后移动一步，快指针每次向后移动两步
    * 3.判断相遇时候的值
    * */
    public static void main(String[] args) {
        if(isHappy(19)){
            System.out.println(true);
        }else{
            System.out.println(false);
        }
    }

    //返回 n 这个数每一位上的平方和
    public static int bitSum(int n){
        int sum = 0;
        while(n > 0){
            int t = n % 10;
            sum += t * t;
            n /= 10;
        }
        return sum;
    }

    public static boolean isHappy(int n){
        int slow = n;
        int fast = bitSum(n);
        while(slow != fast){
            slow = bitSum(slow);
            fast = bitSum(bitSum(fast));
        }
        return slow == 1;
    }

}
